<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :dataSource="dataSource">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                下线
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #keepSession="{ text }">
        <a-tag color="#ccc">{{ text ? '否' : '是' }}</a-tag>
      </template>
      <template #state="{ text }">
        <Icon icon="ant-design:bulb-filled" :style="{ color: text == 1 ? 'green' : 'red' }" />
      </template>
      <template #protocolType="{ text }">
        <a-tag color="blue">{{ text }}</a-tag>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <ConnectModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="connect-list" setup>
  import { reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { list, closeRequest, batchCloseRequest } from './connect.api';
  import { columns, searchFormSchema } from './connect.data';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import ConnectModal from './connectModal.vue';
  const [registerModal, { openModal }] = useModal();
  // 列表页面公共参数、方法
  const { onExportXls, tableContext } = useListPage({
    tableProps: {
      title: '连接管理',
      //   api: list,
      columns: columns,
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  const dataSource = reactive([
    {
      auth: { username: 'fluxmq', password: 'Zmx1eG1x' },
      cleanSession: true,
      clientId: 'hecs-70947_bench_pub_1_2870682444',
      clientIp: '192.168.2.144',
      clientPort: 0,
      connectTime: '2024-03-31 12:25:26',
      id: 101,
      keepalive: 300,
      nodeIp: '192.168.3.77',
      protocolType: 'MQTT',
      state: 1,
      version: 'MQTT_5',
    },
  ]);

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '查看订阅',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '下线',
        popConfirm: {
          title: '是否确认下线？',
          confirm: handleClose.bind(null, record),
        },
      },
    ];
  }

  function handleDetail(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 下线事件
   */
  async function handleClose(record) {
    await closeRequest({ id: record.id, protocolType: record.protocolType }, reload);
  }

  /**
   * 批量下线事件
   */
  async function batchHandleDelete() {
    await batchCloseRequest({ ids: selectedRowKeys.value }, reload);
  }
</script>
